package com.wengzw.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wengzw.eduservice.entity.EduSubject;
import com.wengzw.eduservice.entity.excel.SubjectData;
import com.wengzw.eduservice.entity.subject.OneSubject;
import com.wengzw.eduservice.entity.subject.TwoSubject;
import com.wengzw.eduservice.listener.SubjectExcelListener;
import com.wengzw.eduservice.mapper.EduSubjectMapper;
import com.wengzw.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wengzw
 * @since 2021-06-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
    try {
        InputStream in = file.getInputStream();
        EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
    }catch (Exception e) {
        e.printStackTrace();
    }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //获取一级所有数据,parentid=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //baseMapper是由于继承了ServiceImpl
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);


        //获取二级所有数据
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储封装最终数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来的所有一级分类数据遍历，放到最终list里面
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到oneSubjectList每个EduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //调用方法赋值更简单
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //二级分类
            List<TwoSubject> twoFinalSubject = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类是属于哪个一级分类
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubject.add(twoSubject);
                }
            }
            //吧一级下面的二级分类放到一级分类里
            oneSubject.setChildren(twoFinalSubject);
        }
        return finalSubjectList;
    }
}
